﻿//*************************************************************************************************
// QueryStringExtractionRule.cs
// Owner: Taavi Kõosaar
//
// Extraction Rule, which helps extract parameter values from url query string.
//
// Copyright(c) Microsoft Corporation, 2008
//*************************************************************************************************

using Microsoft.VisualStudio.TestTools.WebTesting;

namespace WebAndLoadTestPluginsAndRules.ExtractionRules
{
    /// <summary>
    /// Extraction Rule, which helps extract parameter values from url query string.
    /// </summary>
    [System.ComponentModel.DisplayName("Extract query string Parameter")]
    [System.ComponentModel.Description("Extract query string parameter.")]
    public class QueryStringExtractionRule : ExtractionRule
    {
        /// <summary>
        /// Gets or sets the name of the query string parameter.
        /// </summary>
        /// <value>The name of the query string param.</value>
        [System.ComponentModel.DisplayName("Parameter name from query string")]
        [System.ComponentModel.Description("The name of the parameter in querystring which value to extract.")]
        public string QueryStringParameterName
        {
            get;
            set;
        }

        /// <summary>
        /// When overridden in a derived class, this method extracts information from a <see cref="T:Microsoft.VisualStudio.TestTools.WebTesting.HtmlDocument"/> and places it into the <see cref="T:Microsoft.VisualStudio.TestTools.WebTesting.WebTestContext"/>.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">An <see cref="T:Microsoft.VisualStudio.TestTools.WebTesting.ExtractionEventArgs"/> that contains the event data.</param>
        public override void Extract(object sender, ExtractionEventArgs e)
        {
            e.Success = false;

            if (e.Response.ResponseUri != null)
            {
                var queryStringValue = GetQueryStringParameterValue(QueryStringParameterName, e);
                if (!string.IsNullOrEmpty(queryStringValue))
                {
                    //If the parameter name already exists, upodate it with a new value
                    if (e.WebTest.Context.ContainsKey(base.ContextParameterName))
                        e.WebTest.Context[base.ContextParameterName] = queryStringValue;
                    else//If it does not exist, add it to context
                        e.WebTest.Context.Add(base.ContextParameterName, queryStringValue);
                    e.Success = true;
                    e.Message = "Extraction of parameter value was successful.";
                }
                else
                {
                    e.Message = string.Format("No parameter was found with the name '{0}'.", QueryStringParameterName);
                }
            }
        }


        /// <summary>
        /// Gets the query string parameter value.
        /// </summary>
        /// <param name="parameterName">Name of the parameter.</param>
        /// <param name="e">The <see cref="Microsoft.VisualStudio.TestTools.WebTesting.ExtractionEventArgs"/> instance containing the event data.</param>
        /// <returns>null or parameter value</returns>
        private static string GetQueryStringParameterValue(string parameterName, ExtractionEventArgs e)
        {
            foreach (QueryStringParameter item in e.Request.QueryStringParameters)
            {
                if (item.Name.Equals(parameterName))
                {
                    return item.Value;
                }
            }
            return null;
        }

    }

}